#include <iostream>
#include <algorithm>
#include <cstring>
#include "Borrow_service_achieve.h"
#include "Borrow_data_achieve.h"
#include "Tool.h"
using namespace std;
using namespace LB;

Borrow_service_achieve::Borrow_service_achieve(void)
{
	
	bor_file = new Borrow_data_achieve;
	bor_file->loadbook(v_classes);
	bor_file->loadusers(v_users);
}

bool Borrow_service_achieve::loginsuccess(int user_id,char* password)
{
	User u1(user_id);
	vector<User>::iterator p = find(v_users.begin(),v_users.end(),u1);
	if (p != v_users.end())
	{
		if(0 == strcmp(p->m_password,password))
		{
			m_user = *p;//set user
			
		 	return true;
		}
		else
		{
			 return false;
		}
	}
	else
	{
		
		return false;
	}
	return false;
}

void Borrow_service_achieve::topborrowbook(void)
{
	//vector<Classbook>::iterator p = v_classes.begin();
	cout << "借书榜:" << endl;
	for (unsigned int i = 0; i < v_classes.size(); i++)
	{	
		if(v_classes[i].v_book.size() != 0)
		{
			Book b = v_classes[i].mostborrowbook();
		cout << "书号："  << b.m_bid << "书名: <<" << b.m_bookname << ">>"<<endl;
		}		
	}	
}

void Borrow_service_achieve::toppraisebook(void)
{	
	vector<Classbook>::iterator p = v_classes.begin();
	cout << "好评榜:" << endl;
	for (;p!=v_classes.end();p++)
	{	
		if( (*p).v_book.size()!= 0 )
		{
			Book b = p->highestpraisebook();	
			cout << "书号："  << b.m_bid 
			 << "  书名: <<" << b.m_bookname << ">>"<< endl;
		}			
	}
}

bool Borrow_service_achieve::findbook(int bookid)
{
	Book book(bookid);
	for(unsigned int i=0;i<v_classes.size();i++)
	{
		if(true == v_classes[i].findbook(book))
		{
			cout << "书名:" << "<<"<<book.m_bookname << ">>"<<endl;
			cout << "书号:" << book.m_bid << endl;
			cout << "在馆状态:" << book.at_library << endl;
			cout << "好评等级: ";
			print_star(book.praise_val);
			cout << "被借次数:" << book.borrow_times << endl;
			cout << "近期评论:" << endl;
			book.showcomment();
			return true;
		}
		else
		{
			cout << "未找到此书籍!" << endl;
			return false;
		}
	}
	cout << "未找到此书籍!" << endl;
	return false;
}

bool Borrow_service_achieve::findbook(char* bookname)
{
	bool flag = false;
	for(unsigned int i=0;i < v_classes.size();i++)
	{
		for(unsigned int j=0;j<v_classes[i].v_book.size();j++)
		{
			if(0 == strcmp(bookname,v_classes[i].v_book[j].m_bookname))
			{
				flag = true;
				Book findbook = v_classes[i].v_book[j];
				cout << "书名: <<" << findbook.m_bookname << ">>"<<endl;
				cout << "书号:" << findbook.m_bid << endl;
				cout << "在馆状态:" << boolalpha<<findbook.at_library << endl;
				cout << "好评等级:";
				print_star(findbook.praise_val);
				cout << "被借次数:" << findbook.borrow_times << endl;
				cout << "近期评论:" << endl;
				findbook.showcomment();
				cout << endl;
			}
		}
	}
	if(false == flag)
	{
		cout << "未找到此书籍!" << endl;
		cout << "以下可能有您要找的书籍:"<<endl;
	}
	
	if(findkeyword(bookname)) flag = true;

	if(!flag) cout << "无您想要的书　！" << endl;
	return flag;
}

bool Borrow_service_achieve::findkeyword(char* keyword)
{
	bool flag = false;
	for(unsigned int i=0;i < v_classes.size();i++)
	{
		for(unsigned int j=0;j<v_classes[i].v_book.size();j++)
		{
			if(true == v_classes[i].v_book[j].findkey(keyword))
			{
				flag = true;
				cout << endl;
				Book findbook = v_classes[i].v_book[j];
				cout << "书名:" << findbook.m_bookname << endl;
				cout << "书号:" << findbook.m_bid << endl;
				cout << "在馆状态:" << boolalpha <<findbook.at_library << endl;
				cout << endl;
			}
		}
	}
	return flag;
}

void Borrow_service_achieve::scorebook(int bookid,int score,char* comment)
{
	for(unsigned int i=0;i<v_classes.size();i++)
	{
		for(unsigned int j=0;j<v_classes[i].v_book.size();j++)
		{
			if(bookid == v_classes[i].v_book[j].m_bid)//get book
			{			
				float p = v_classes[i].v_book[j].praise_val;
				int t = v_classes[i].v_book[j].borrow_times;
				if( t != 0)
				{
					v_classes[i].v_book[j].praise_val = (p*(t-1)+score)/(t*1.0);
				}
				v_classes[i].v_book[j].praise_val = 0;

				v_classes[i].v_book[j].insertcomment(m_user.m_uid,comment);		
			}
		}
	}
}

void Borrow_service_achieve::listclass(void)
{
	for (unsigned int i = 0; i < v_classes.size(); ++i)
	{
		cout << " " << v_classes[i].m_cid 
			 << " " << v_classes[i].m_classname << endl;
	}
}

void Borrow_service_achieve::listbook(int classid)
{
	for (unsigned int i = 0; i < v_classes.size(); ++i)
	{
		if (classid == v_classes[i].m_cid)
		{
			cout << " " << v_classes[i].m_cid 
			 << "   " << v_classes[i].m_classname << endl;	

			for (unsigned int j = 0;j < v_classes[i].v_book.size();j++)
			{
				cout << "  " << v_classes[i].v_book[j].m_bid
					 << " <<" << v_classes[i].v_book[j].m_bookname << ">>"
					 << " "	<< ((v_classes[i].v_book[j].at_library) ? "在馆":"离馆")<< endl;
					
			}
		}
	}
}

void Borrow_service_achieve::listmymsg(void)
{
	cout << m_user;
}

Borrow_service_achieve::~Borrow_service_achieve(void)
{
	bor_file->savebook(v_classes);
	bor_file->saveusers(v_users);
	delete bor_file;
}
